<template>
    <div class="home_container">
      <van-nav-bar class="page-nav-bar">
        <van-button class="search_btn" slot="title" round type="info" icon="search" to="/search">搜索</van-button>
      </van-nav-bar>
      <!-- tabs标签页 -->
      <van-tabs class="home-tabs" v-model="active" animated swipeable>
        <van-tab :key="item.id" v-for="item in channels" :title="item.name">
          <!-- 使用组件 -->
          <article-list :channel="item" />
        </van-tab>
        <div class="placehoder_box" slot="nav-right"></div>
        <!-- 汉堡按钮 -->
        <div @click="isShowEditPopup = true" class="hamburger_btn" slot="nav-right">
          <i class="heima hm-gengduo"></i>
        </div>
      </van-tabs>
      <!-- tabs标签页 -->
      <!-- 编辑频道弹出层 -->
        <van-popup
          v-model="isShowEditPopup"
          closeable
          position="bottom"
          :style="{ height: '90%' }"
        >
        <channelEdit @updateActive="onUpdateActive" :my-channels="channels" :active="active"></channelEdit>
        </van-popup>
      <!-- 编辑频道弹出层 -->
    </div>
</template>

<script>
import { getChannels } from '@/api/user.js'
import ArticleList from './components/article-list'
import ChannelEdit from './components/channelEdit'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'
export default {
  name: 'home',
  data () {
    return {
      active: 0,
      channels: [],
      isShowEditPopup: false
    }
  },
  components: {
    ArticleList,
    ChannelEdit
  },
  created () {
    this.loadChannels()
  },
  computed: {
    ...mapState(['user'])
  },
  methods: {
    // 加载首页用户频道列表
    async loadChannels () {
      try {
        // 如果用户登录，发送请求获取回来
        // 如果用户未登录
        // 但是本地没有存储数据 发送请求获取数据
        // 本地有数据 获取本地的数据
        // 如果用户未登录且本地还有数据 获取本地数据
        const localChannels = getItem('hmtt_channels')
        if (!this.user && localChannels) {
          this.channels = localChannels
          return false
        }
        const { data: res } = await getChannels()
        this.channels = res.data.channels
      } catch (err) {
        this.$toast.fail('获取频道列表数据失败')
      }
    },
    onUpdateActive (index, flag) {
      // 更新激活的频道项
      this.active = index
      // 关闭编辑频道的弹层
      this.isShowEditPopup = flag
    }
  }
}
</script>

<style scoped lang='less'>
.home_container {
  padding-bottom: 100px;
  // 凡是遇到组件内部的类名样式修改不起作用，在前面加上、/deep/
  /deep/.van-nav-bar {
    .van-nav-bar__title {
      max-width: unset;
    }
  }
  .search_btn {
    width: 555px;
    height: 64px;
    background: rgba(255, 255, 255, .2);
    border-radius: 32px;
  }
  .van-nav-bar .van-icon {
    color: #fff;
  }
  /deep/.home-tabs {
    .van-tabs__nav {
      padding-bottom: 0;
      height: 80px;
    }
    .van-tab {
      min-width: 190px;
      border-right: 1px solid #eee;
    }
    .van-tabs__line {
      width: 31px;
      height: 6px;
      background: #3295f9;
      border-radius: 3px;
      bottom: 8px;
    }
  }
  .hamburger_btn {
    width: 66px;
    height: 82px;
    background: #fff;
    position: fixed;
    right: 0;
    text-align: center;
    line-height: 82px;
    opacity: 0.9;
    .hm-gengduo {
      font-size: 40px;
    }
    &:before {
      content: '';
      position: absolute;
      left: 0;
      width: 2px;
      background-image: url(~@/assets/gradient-gray-line.png);
      background-size: contain;
    }
  }
  .placehoder_box {
    width: 66px;
    height: 82px;
    flex-shrink: 0;
  }
}
</style>
